package net.thumbtack.onepm.tests.netJiraTrac;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.concurrent.TimeUnit;

import static org.junit.Assert.fail;

/**
 * Created with IntelliJ IDEA.
 * User: efrolova
 * Date: 01.10.12
 * Time: 11:11
 * To change this template use File | Settings | File Templates.
 */
public class TestBaseJiraTrac {
    static WebDriver driver;
    static String baseurlRedmine;
    static String baseurlJira;
    static String baseurlTrac;
    private static StringBuffer verificationErrors = new StringBuffer();


  protected String projectName ="Project Testing number #";
    protected String projectDescription="Project for testing convert from Jira # ";
  protected String projectID="abc";
    protected String AdminLogin="admin";
    protected String PasswordAd="admin";
    protected String PasswordAdmTrac="123456";
     protected String IssueSummary= "This is issues about positive tests #";
    protected String issueDescription ="This is decsriptionssss for test #";
    protected String Envieroment= "production #";
    protected String  TimetrackOrig="1";
    protected String TimetrackRec ="1";
      protected int i=1;
    @BeforeClass
    public static void setUp() throws Exception {
       driver = new FirefoxDriver();

      //  WebDriver driver = new RemoteWebDriver(
       //         new URL(""), DesiredCapabilities.firefox());
        baseurlJira="http://jira.thumbtack.lo:8080/secure/Dashboard.jspa";
        baseurlTrac="http://jira.thumbtack.lo:8002/Debian.World.Ru";
        baseurlRedmine = " http://jira.thumbtack.lo:3000";
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

    }
    @AfterClass
    public static void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }
    protected static boolean isElementPresent(By by) {
        try {
            TestBaseJiraTrac.driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }

    protected void fillFormProject(Projects projects) throws InterruptedException {
        TestBaseJiraTrac.driver.findElement(By.id("project_name")).clear();
        TestBaseJiraTrac.driver.findElement(By.id("project_name")).sendKeys(projects.getProjectName());
        TestBaseJiraTrac.driver.findElement(By.id("project_description")).clear();
        TestBaseJiraTrac.driver.findElement(By.id("project_description")).sendKeys(projects.getProjectDescription());

        TestBaseJiraTrac.driver.findElement(By.id("project_identifier")).clear();
        TestBaseJiraTrac.driver.findElement(By.id("project_identifier")).sendKeys(projects.getProjectID());

    }
    protected void fillFormAdmJiraRedm(AdminAll adminAll)  throws InterruptedException{
        TestBaseJiraTrac.driver.findElement(By.id("login-form-username")).clear();
        TestBaseJiraTrac.driver.findElement(By.id("login-form-username")).sendKeys(adminAll.getEmail());
        TestBaseJiraTrac.driver.findElement(By.id("login-form-password")).clear();
        TestBaseJiraTrac.driver.findElement(By.id("login-form-password")).sendKeys(adminAll.getPasswordAd());
    }
    protected void fillFormAdmTrac (AdminAll adminAll)  throws InterruptedException{
        TestBaseJiraTrac.driver.findElement(By.id("email")).clear();
        TestBaseJiraTrac.driver.findElement(By.id("email")).sendKeys(adminAll.getEmail());
        TestBaseJiraTrac.driver.findElement(By.id("password")).clear();
        TestBaseJiraTrac.driver.findElement(By.id("password")).sendKeys(adminAll.getPasswordAdTrac());

    }

    protected void OptimazeProject( Projects[] array){
        int FirstInd=1;
        int EndInd=100;

        for(int i = FirstInd; i < EndInd; i++) {
            array[i] = new Projects(projectName+i,projectDescription+i, projectID+i);
        } }
    protected void OptimazeIssue( Issuess[] array){
        int FirstInd=1;
        int EndInd=100;

        for(int i = FirstInd; i < EndInd; i++) {
            array[i] = new Issuess(IssueSummary+i,Envieroment+i, issueDescription+i,TimetrackOrig+i+"w",TimetrackRec+i+"w");
        } }
            //in class OptimazeProject(Project);
     //   Project[i]=Project[i+19];
        //    driver.findElement(By.name("continue")).click();
      protected void fillFormIssueJira(Issuess issue) throws InterruptedException {

          TestBaseJiraTrac.driver.findElement(By.id("project-field")).click();
          Thread.sleep(100);
          TestBaseJiraTrac.driver.findElement(By.id("project-field")).sendKeys(Keys.ARROW_DOWN);
          Thread.sleep(1500);
        driver.findElement(By.id("project-field")).sendKeys(Keys.TAB);
          Thread.sleep(100);
      driver.findElement(By.id("issuetype-field")).click();
          Thread.sleep(1000);

    // driver.findElement(By.id("issuetype-field")).sendKeys(Keys.DOWN);
          Thread.sleep(500);
        driver.findElement(By.id("issuetype-field")).sendKeys(Keys.TAB);
          Thread.sleep(500);
        driver.findElement(By.id("summary")).clear();
        driver.findElement(By.id("summary")).sendKeys(issue.getIssueSummary());
        driver.findElement(By.id("priority-field")).click();
        driver.findElement(By.linkText("Critical")).click();
        driver.findElement(By.id("duedate")).sendKeys("16/Nov/12");
        driver.findElement(By.id("environment")).clear();
        driver.findElement(By.id("environment")).sendKeys(issue.getEnvieroment());
        driver.findElement(By.id("description")).clear();
        driver.findElement(By.id("description")).sendKeys(issue.getIssueDescription());
        driver.findElement(By.id("timetracking_originalestimate")).clear();
        driver.findElement(By.id("timetracking_originalestimate")).sendKeys(issue.getIssueTimeOrig());
        driver.findElement(By.id("timetracking_remainingestimate")).clear();
        driver.findElement(By.id("timetracking_remainingestimate")).sendKeys(issue.getIssueTimeRec());



    }
    protected void fillFormIssueTrac(Issuess issue) throws InterruptedException {
      //  driver.findElement(By.linkText("New Ticket")).click();
    //    driver.get(baseurlTrac + "/newticket");
        driver.findElement(By.id("field-summary")).clear();
        driver.findElement(By.id("field-summary")).sendKeys(issue.getIssueSummary());
        driver.findElement(By.id("field-description")).clear();
        driver.findElement(By.id("field-description")).sendKeys(issue.getIssueDescription());
        new Select(driver.findElement(By.id("field-type"))).selectByVisibleText("defect");
        new Select(driver.findElement(By.id("field-priority"))).selectByVisibleText("critical");
        new Select(driver.findElement(By.id("field-version"))).selectByVisibleText("2.0");
        driver.findElement(By.id("field-cc")).clear();
        driver.findElement(By.id("field-cc")).sendKeys("rrrr");
        new Select(driver.findElement(By.id("field-estimate"))).selectByVisibleText("0.25");
        driver.findElement(By.id("field-actual")).clear();
        driver.findElement(By.id("field-actual")).sendKeys("yes");
        driver.findElement(By.id("field-blocking")).clear();
        driver.findElement(By.id("field-blocking")).sendKeys("yes");

    }
    }